/*********************************************************
 *  Copyright (c) 2011 by Web Information Systems (WIS) Group, 
 *  Delft University of Technology.
 *  Qi Gao, http://wis.ewi.tudelft.nl/index.php/home-qi-gao
 *  
 *  Some rights reserved.
 *
 *  Contact: q.gao@tudelft.nl
 *
 **********************************************************/
package org.persweb.genius;

import java.util.HashMap;
import java.util.List;

import org.persweb.genius.model.Concept;
import org.persweb.genius.model.Filter;
import org.persweb.genius.model.InterestsProfileImpl;
import org.persweb.genius.model.Item;

/**
 * @author Qi Gao <a href="mailto:q.gao@tudelft.nl">q.gao@tudelft.nl</a>
 * @version created on Jun 27, 2011 11:08:48 AM
 */
public class GenericModelingStrategy implements ModelingStrategy {

	/** the configuration of this strategy */
	private GeniusConfiguration conf = null;
	
	/**
	 * the filter for the profile construction
	 */
	private Filter filter = null;

	/**
	 * @param conf
	 *            the configuration of this modeling strategy specifies the type
	 *            of profile, etc.
	 */
	public GenericModelingStrategy(GeniusConfiguration conf) {
		super();
		this.conf = conf;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.wis.twitter.genius.ModelingStrategy#getProfile(java.sql.Timestamp,
	 * java.sql.Timestamp)
	 */
	@Override
	public InterestsProfileImpl getProfile(GeniusConfiguration conf, Filter filter) {

		InterestsProfileImpl profile = new InterestsProfileImpl();

		// fetch the item
		List<Item> items;
		if (filter != null && (filter.getUserIds() != null
				|| filter.getStartTime() != null
				|| filter.getEndTime() != null
				|| filter.getQueries() != null)) {
			items = conf.itemsFetcher.getItems(filter);
		} else {
			items = conf.itemsFetcher.getItems();
		}

		List<Item> enrichedItems = null;
		if (filter != null && filter.getQueries() != null) {
			enrichedItems = conf.enrichment.enrich(items, filter);
		} else {
			enrichedItems = conf.enrichment.enrich(items);
		}

		profile
				.setInterestsProfile((HashMap<Concept, Double>) conf.weightFuction
						.calculateWeights(enrichedItems));

		System.out.println("size of generated profile: " + profile.size());
		return profile;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.wis.twitter.genius.ModelingStrategy#getConfiguration()
	 */
	@Override
	public GeniusConfiguration getConfiguration() {
		return this.conf;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.wis.twitter.genius.ModelingStrategy#setConfiguration(org.wis.twitter
	 * .genius.GMConfiguration)
	 */
	@Override
	public void setConfiguration(GeniusConfiguration conf) {
		this.conf = conf;
	}

	/* (non-Javadoc)
	 * @see org.persweb.genius.ModelingStrategy#getFilter()
	 */
	@Override
	public Filter getFilter() {
		return this.filter;
	}

	/* (non-Javadoc)
	 * @see org.persweb.genius.ModelingStrategy#setFilter(org.persweb.genius.model.Filter)
	 */
	@Override
	public void setFilter(Filter filter) {
		this.filter = filter;
	}

}